#include "controlbar.h"
#include <QDebug>
#include <iostream>

ControlBar::ControlBar(QWidget *parent) : QWidget(parent)
{
    timeSlider = new QSlider(this);
    timeSlider->setOrientation(Qt::Horizontal);
    timeSlider->setSingleStep(1);
    timeSlider->setMaximum(200);
    volumeSlider = new QSlider(this);
    volumeSlider->setOrientation(Qt::Horizontal);
    volumeSlider->setMaximum(100);
    volumeSlider->setValue(100);
    volumeSlider->setMaximumWidth(70);
    muteButton = new QToolButton(this);
    muteButton->setIcon(QIcon("volume.png"));
    timeLabel = new QLabel("2:37", this);
    StartStopButton = new QPushButton(QIcon("play2.png"), "", this);
    StartStopButton->setIconSize(QSize(30, 30));
    StartStopButton->setFixedSize(40, 40);
    previousButton = new QPushButton(QIcon("previous.png"), "", this);
    previousButton->setIconSize(QSize(30, 30));
    previousButton->setFixedSize(40, 40);
    nextButton = new QPushButton(QIcon("next.png"), "", this);
    nextButton->setIconSize(QSize(30, 30));
    nextButton->setFixedSize(40, 40);

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->addSpacing(15);
    layout->addWidget(previousButton);
    layout->addWidget(StartStopButton);
    layout->addWidget(nextButton);
    layout->addSpacing(35);
    layout->addWidget(timeSlider);
    layout->addWidget(timeLabel);
    layout->addSpacing(40);
    layout->addWidget(muteButton);
    layout->addWidget(volumeSlider);
    setLayout(layout);


    player = qobject_cast<QMediaPlayer*>(VideoPlayer::get()->mediaObject());

    QObject::connect(nextButton, SIGNAL(clicked()), this, SIGNAL(nextSong()));
    QObject::connect(previousButton, SIGNAL(clicked()), this, SIGNAL(previousSong()));
    QObject::connect(volumeSlider, SIGNAL(valueChanged(int)), this, SLOT(changeVolume(int)));
    QObject::connect(timeSlider, SIGNAL(sliderMoved(int)), this, SLOT(onTimeChange(int)));
    QObject::connect(StartStopButton, SIGNAL(clicked()), this, SLOT(resumePause()));
    QObject::connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(updateTime(qint64)));
    QObject::connect(muteButton, SIGNAL(clicked()), this, SLOT(volumeClicked()));
}

void ControlBar::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void ControlBar::resumePause()
{
    QMediaPlayer::State currentState = player->state();
    if(currentState == QMediaPlayer::PlayingState)
    {
        StartStopButton->setIcon(QIcon("play2.png"));
        StartStopButton->setToolTip("Play");
        player->pause();

    }
    else if(currentState == QMediaPlayer::PausedState)
    {
        StartStopButton->setIcon(QIcon("pause2.png"));
        StartStopButton->setToolTip("Pause");
        player->play();
    }
}

void ControlBar::nextSong()
{

}

void ControlBar::previousSong()
{

}

void ControlBar::volumeClicked()
{
    if(player->isMuted())
    {
        muteButton->setIcon(QIcon("volume.png"));
        player->setMuted(false);
    }
    else
    {
        muteButton->setIcon(QIcon("mute.png"));
        player->setMuted(true);
    }
}

void ControlBar::changeVolume(int newVolume)
{
    player->setVolume(newVolume);
}

void ControlBar::initNewSong(VideoInfo vid)
{
    QTime duration(0, vid.duration/60, vid.duration % 60);
    qDebug() << duration.toString("m:ss");
    timeLabel->setText(duration.toString("m:ss"));
    timeSlider->setMaximum(vid.duration*1000);
    StartStopButton->setIcon(QIcon("pause2.png"));
    StartStopButton->setToolTip("Pause");
}

void ControlBar::updateTime(qint64 time)
{
    timeSlider->setValue(time);
}

void ControlBar::onTimeChange(int newTime)
{
    if(player->isSeekable())
        player->setPosition(newTime);
    else
        timeSlider->setValue(0);
}
